<?php
namespace app\system\controller;

use app\common\model\ShopJicaiClassModel;
use app\common\model\ShopJicaiGoodsModel;
use think\Request;

class ShopJicaiClassController extends BaseController
{
    private $jicaiClassModel;
    private $jicaiModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->jicaiClassModel = new ShopJicaiClassModel();
        $this->jicaiModel = new ShopJicaiGoodsModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map       = [];
        $query     = [];
        $className = $this->request->param('class_name');
        if(empty($className)){
            $map['class_name'] = ['like', '%' . $className . '%'];
            $query['class_name'] = $className;
        }
        $list = $this->jicaiClassModel->where($map)->order('class_sort asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $result = $this->validate($data,  [
                'class_name|分类名称'    => 'require',
            ]);
            if ($result !== true) {
                $this->error($result);
            }
            $this->jicaiClassModel->addData($data);
            $this->success('添加成功', url('index'));
        }
        return $this->fetch();
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            $result = $this->validate($data, [
                'class_name|分类名称'    => 'require',
            ]);
            if ($result !== true) {
                $this->error($result);
            }
            $this->jicaiClassModel->editData($data);
            if($data['class_name'] != $this->jicaiModel->field('class_name')->where('class_id',$data['class_id'])->find()){
                $this->jicaiModel->where('class_id',$data['class_id'])->setField('class_name',$data['class_name']);
            }
            $this->success('操作成功！', url('index'));
        }
        $classId = $this->request->param('class_id');
        $info    = $this->jicaiClassModel->where('class_id', $classId)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $classId = $this->request->param('class_id/d');
            if ($this->jicaiModel->where('class_id', $classId)->find()) {
                $this->error('该分类下还存在集采商品，不允许删除！');
            }
            $this->jicaiClassModel->delData($classId);
            $this->success('删除成功！');
        }
    }

    /**
     *排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $class_id => $class_sort) {
            $classId    = intval($class_id);
            $classSort  = intval($class_sort);
            $dataList[] = ["class_id" => $classId, "class_sort" => $classSort];
        }
        $this->jicaiClassModel->saveAll($dataList);
        $this->success('排序成功！');
    }


}
